package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.common.PageResult;
import org.example.common.Result;
import org.example.dto.*;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * 
 * @author example
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/users")
@Validated
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 创建用户
     */
    @ApiOperation("创建用户")
    @PostMapping
    public ResponseEntity<Result<User>> createUser(
            @ApiParam("用户创建信息") @Valid @RequestBody UserCreateDTO createDTO) {
        User user = userService.createUser(createDTO);
        return ResponseEntity.ok(Result.success(user, "用户创建成功"));
    }
    
    /**
     * 获取所有用户
     */
    @ApiOperation("获取所有用户")
    @GetMapping
    public ResponseEntity<Result<List<User>>> getAllUsers() {
        List<User> users = userService.findAllUsers();
        return ResponseEntity.ok(Result.success(users, "获取用户列表成功"));
    }
    
    /**
     * 分页查询用户
     */
    @ApiOperation("分页查询用户")
    @GetMapping("/page")
    public ResponseEntity<Result<PageResult<User>>> getUsersWithPage(
            @ApiParam("分页查询参数") @Valid UserPageDTO pageDTO) {
        PageResult<User> result = userService.findUsersWithPage(pageDTO);
        return ResponseEntity.ok(Result.success(result, "分页查询成功"));
    }
    
    /**
     * 根据ID获取用户
     */
    @ApiOperation("根据ID获取用户")
    @GetMapping("/{id}")
    public ResponseEntity<Result<User>> getUserById(
            @ApiParam("用户ID") @PathVariable @NotNull Long id) {
        User user = userService.findById(id);
        if (user != null) {
            return ResponseEntity.ok(Result.success(user, "获取用户信息成功"));
        } else {
            return ResponseEntity.ok(Result.notFound("用户不存在"));
        }
    }
    
    /**
     * 根据用户名搜索用户
     */
    @ApiOperation("根据用户名搜索用户")
    @GetMapping("/search")
    public ResponseEntity<Result<List<User>>> searchUsers(
            @ApiParam("用户名关键字") @RequestParam String username) {
        List<User> users = userService.searchByUsername(username);
        return ResponseEntity.ok(Result.success(users, "搜索用户成功"));
    }
    
    /**
     * 更新用户
     */
    @ApiOperation("更新用户信息")
    @PutMapping("/{id}")
    public ResponseEntity<Result<User>> updateUser(
            @ApiParam("用户ID") @PathVariable @NotNull Long id,
            @ApiParam("用户更新信息") @Valid @RequestBody UserUpdateDTO updateDTO) {
        User user = userService.updateUser(id, updateDTO);
        return ResponseEntity.ok(Result.success(user, "用户更新成功"));
    }
    
    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> deleteUser(
            @ApiParam("用户ID") @PathVariable @NotNull Long id) {
        userService.deleteUser(id);
        return ResponseEntity.ok(Result.success(null, "用户删除成功"));
    }
    
    /**
     * 批量操作用户
     */
    @ApiOperation("批量操作用户")
    @PostMapping("/batch")
    public ResponseEntity<Result<Void>> batchOperation(
            @ApiParam("批量操作参数") @Valid @RequestBody UserBatchDTO batchDTO) {
        userService.batchOperation(batchDTO);
        return ResponseEntity.ok(Result.success(null, "批量操作成功"));
    }
    
    /**
     * 获取用户统计信息
     */
    @ApiOperation("获取用户统计信息")
    @GetMapping("/stats")
    public ResponseEntity<Result<Map<String, Object>>> getUserStats() {
        Map<String, Object> stats = userService.getUserStats();
        return ResponseEntity.ok(Result.success(stats, "获取用户统计成功"));
    }
}